import { IBasicBlog, IBlog, IBlogComment, IBlogNestedComment } from '@/types/blog.ts'

export const getBasicBlog = (blog: IBlog): IBasicBlog => {
  return {
    category: blog.category,
    isGreat: blog.isGreat,
    readDuration: blog.readDuration,
    topValue: blog.topValue,
    wordCount: blog.wordCount,
    visitCount: blog.visitCount,
    createTime: blog.createTime,
    updateTime: blog.updateTime
  }
}

// 扁平化数据转两层嵌套结构
export function flattenToTwoLevel(comments: IBlogComment[]): IBlogNestedComment[] {
  // 创建评论映射表，快速查找评论
  const commentMap = new Map<number, IBlogComment>()
  comments.forEach((comment) => commentMap.set(comment.id, comment))

  // 结果数组
  const result: IBlogNestedComment[] = []

  // 遍历所有评论，构建两层结构
  comments.forEach((comment) => {
    if (comment.parentId === 0) {
      // 顶层评论
      const replies = comments.filter((reply) => reply.parentId === comment.id)
      result.push({ comment, replies })
    }
  })

  return result
}
